{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-alpha0\n",
      "sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)\n",
      "matplotlib 3.0.3\n",
      "numpy 1.16.2\n",
      "pandas 0.24.2\n",
      "sklearn 0.20.3\n",
      "tensorflow 2.0.0-alpha0\n",
      "tensorflow.python.keras.api._v2.keras 2.2.4-tf\n"
     ]
    }
   ],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sklearn\n",
    "import pandas as pd\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import tensorflow as tf\n",
    "\n",
    "from tensorflow import keras\n",
    "\n",
    "print(tf.__version__)\n",
    "print(sys.version_info)\n",
    "for module in mpl, np, pd, sklearn, tf, keras:\n",
    "    print(module.__name__, module.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['./generate_csv/train_00.csv',\n",
      " './generate_csv/train_01.csv',\n",
      " './generate_csv/train_02.csv',\n",
      " './generate_csv/train_03.csv',\n",
      " './generate_csv/train_04.csv',\n",
      " './generate_csv/train_05.csv',\n",
      " './generate_csv/train_06.csv',\n",
      " './generate_csv/train_07.csv',\n",
      " './generate_csv/train_08.csv',\n",
      " './generate_csv/train_09.csv',\n",
      " './generate_csv/train_10.csv',\n",
      " './generate_csv/train_11.csv',\n",
      " './generate_csv/train_12.csv',\n",
      " './generate_csv/train_13.csv',\n",
      " './generate_csv/train_14.csv',\n",
      " './generate_csv/train_15.csv',\n",
      " './generate_csv/train_16.csv',\n",
      " './generate_csv/train_17.csv',\n",
      " './generate_csv/train_18.csv',\n",
      " './generate_csv/train_19.csv']\n",
      "['./generate_csv/valid_00.csv',\n",
      " './generate_csv/valid_01.csv',\n",
      " './generate_csv/valid_02.csv',\n",
      " './generate_csv/valid_03.csv',\n",
      " './generate_csv/valid_04.csv',\n",
      " './generate_csv/valid_05.csv',\n",
      " './generate_csv/valid_06.csv',\n",
      " './generate_csv/valid_07.csv',\n",
      " './generate_csv/valid_08.csv',\n",
      " './generate_csv/valid_09.csv']\n",
      "['./generate_csv/test_00.csv',\n",
      " './generate_csv/test_01.csv',\n",
      " './generate_csv/test_02.csv',\n",
      " './generate_csv/test_03.csv',\n",
      " './generate_csv/test_04.csv',\n",
      " './generate_csv/test_05.csv',\n",
      " './generate_csv/test_06.csv',\n",
      " './generate_csv/test_07.csv',\n",
      " './generate_csv/test_08.csv',\n",
      " './generate_csv/test_09.csv']\n"
     ]
    }
   ],
   "source": [
    "source_dir = \"./generate_csv/\"\n",
    "\n",
    "def get_filenames_by_prefix(source_dir, prefix_name):\n",
    "    all_files = os.listdir(source_dir)\n",
    "    results = []\n",
    "    for filename in all_files:\n",
    "        if filename.startswith(prefix_name):\n",
    "            results.append(os.path.join(source_dir, filename))\n",
    "    return results\n",
    "\n",
    "train_filenames = get_filenames_by_prefix(source_dir, \"train\")\n",
    "valid_filenames = get_filenames_by_prefix(source_dir, \"valid\")\n",
    "test_filenames = get_filenames_by_prefix(source_dir, \"test\")\n",
    "\n",
    "import pprint\n",
    "pprint.pprint(train_filenames)\n",
    "pprint.pprint(valid_filenames)\n",
    "pprint.pprint(test_filenames)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_csv_line(line, n_fields = 9):\n",
    "    defs = [tf.constant(np.nan)] * n_fields\n",
    "    parsed_fields = tf.io.decode_csv(line, record_defaults=defs)\n",
    "    x = tf.stack(parsed_fields[0:-1])\n",
    "    y = tf.stack(parsed_fields[-1:])\n",
    "    return x, y\n",
    "\n",
    "def csv_reader_dataset(filenames, n_readers=5,\n",
    "                       batch_size=32, n_parse_threads=5,\n",
    "                       shuffle_buffer_size=10000):\n",
    "    dataset = tf.data.Dataset.list_files(filenames)\n",
    "    dataset = dataset.repeat()\n",
    "    dataset = dataset.interleave(\n",
    "        lambda filename: tf.data.TextLineDataset(filename).skip(1),\n",
    "        cycle_length = n_readers\n",
    "    )\n",
    "    dataset.shuffle(shuffle_buffer_size)\n",
    "    dataset = dataset.map(parse_csv_line,\n",
    "                          num_parallel_calls=n_parse_threads)\n",
    "    dataset = dataset.batch(batch_size)\n",
    "    return dataset\n",
    "\n",
    "batch_size = 32\n",
    "train_set = csv_reader_dataset(train_filenames,\n",
    "                               batch_size = batch_size)\n",
    "valid_set = csv_reader_dataset(valid_filenames,\n",
    "                               batch_size = batch_size)\n",
    "test_set = csv_reader_dataset(test_filenames,\n",
    "                              batch_size = batch_size)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def serialize_example(x, y):\n",
    "    \"\"\"Converts x, y to tf.train.Example and serialize\"\"\"\n",
    "    input_feautres = tf.train.FloatList(value = x)\n",
    "    label = tf.train.FloatList(value = y)\n",
    "    features = tf.train.Features(\n",
    "        feature = {\n",
    "            \"input_features\": tf.train.Feature(\n",
    "                float_list = input_feautres),\n",
    "            \"label\": tf.train.Feature(float_list = label)\n",
    "        }\n",
    "    )\n",
    "    example = tf.train.Example(features = features)\n",
    "    return example.SerializeToString()\n",
    "\n",
    "def csv_dataset_to_tfrecords(base_filename, dataset,\n",
    "                             n_shards, steps_per_shard,\n",
    "                             compression_type = None):\n",
    "    options = tf.io.TFRecordOptions(\n",
    "        compression_type = compression_type)\n",
    "    all_filenames = []\n",
    "    for shard_id in range(n_shards):\n",
    "        filename_fullpath = '{}_{:05d}-of-{:05d}'.format(\n",
    "            base_filename, shard_id, n_shards)\n",
    "        with tf.io.TFRecordWriter(filename_fullpath, options) as writer:\n",
    "            for x_batch, y_batch in dataset.take(steps_per_shard):\n",
    "                for x_example, y_example in zip(x_batch, y_batch):\n",
    "                    writer.write(\n",
    "                        serialize_example(x_example, y_example))\n",
    "        all_filenames.append(filename_fullpath)\n",
    "    return all_filenames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_shards = 20\n",
    "train_steps_per_shard = 11610 // batch_size // n_shards\n",
    "valid_steps_per_shard = 3880 // batch_size // n_shards\n",
    "test_steps_per_shard = 5170 // batch_size // n_shards\n",
    "\n",
    "output_dir = \"generate_tfrecords\"\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "\n",
    "train_basename = os.path.join(output_dir, \"train\")\n",
    "valid_basename = os.path.join(output_dir, \"valid\")\n",
    "test_basename = os.path.join(output_dir, \"test\")\n",
    "\n",
    "train_tfrecord_filenames = csv_dataset_to_tfrecords(\n",
    "    train_basename, train_set, n_shards, train_steps_per_shard, None)\n",
    "valid_tfrecord_filenames = csv_dataset_to_tfrecords(\n",
    "    valid_basename, valid_set, n_shards, valid_steps_per_shard, None)\n",
    "test_tfrecord_fielnames = csv_dataset_to_tfrecords(\n",
    "    test_basename, test_set, n_shards, test_steps_per_shard, None)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_shards = 20\n",
    "train_steps_per_shard = 11610 // batch_size // n_shards\n",
    "valid_steps_per_shard = 3880 // batch_size // n_shards\n",
    "test_steps_per_shard = 5170 // batch_size // n_shards\n",
    "\n",
    "output_dir = \"generate_tfrecords_zip\"\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "\n",
    "train_basename = os.path.join(output_dir, \"train\")\n",
    "valid_basename = os.path.join(output_dir, \"valid\")\n",
    "test_basename = os.path.join(output_dir, \"test\")\n",
    "\n",
    "train_tfrecord_filenames = csv_dataset_to_tfrecords(\n",
    "    train_basename, train_set, n_shards, train_steps_per_shard,\n",
    "    compression_type = \"GZIP\")\n",
    "valid_tfrecord_filenames = csv_dataset_to_tfrecords(\n",
    "    valid_basename, valid_set, n_shards, valid_steps_per_shard,\n",
    "    compression_type = \"GZIP\")\n",
    "test_tfrecord_fielnames = csv_dataset_to_tfrecords(\n",
    "    test_basename, test_set, n_shards, test_steps_per_shard,\n",
    "    compression_type = \"GZIP\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['generate_tfrecords_zip/train_00000-of-00020',\n",
      " 'generate_tfrecords_zip/train_00001-of-00020',\n",
      " 'generate_tfrecords_zip/train_00002-of-00020',\n",
      " 'generate_tfrecords_zip/train_00003-of-00020',\n",
      " 'generate_tfrecords_zip/train_00004-of-00020',\n",
      " 'generate_tfrecords_zip/train_00005-of-00020',\n",
      " 'generate_tfrecords_zip/train_00006-of-00020',\n",
      " 'generate_tfrecords_zip/train_00007-of-00020',\n",
      " 'generate_tfrecords_zip/train_00008-of-00020',\n",
      " 'generate_tfrecords_zip/train_00009-of-00020',\n",
      " 'generate_tfrecords_zip/train_00010-of-00020',\n",
      " 'generate_tfrecords_zip/train_00011-of-00020',\n",
      " 'generate_tfrecords_zip/train_00012-of-00020',\n",
      " 'generate_tfrecords_zip/train_00013-of-00020',\n",
      " 'generate_tfrecords_zip/train_00014-of-00020',\n",
      " 'generate_tfrecords_zip/train_00015-of-00020',\n",
      " 'generate_tfrecords_zip/train_00016-of-00020',\n",
      " 'generate_tfrecords_zip/train_00017-of-00020',\n",
      " 'generate_tfrecords_zip/train_00018-of-00020',\n",
      " 'generate_tfrecords_zip/train_00019-of-00020']\n",
      "['generate_tfrecords_zip/valid_00000-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00001-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00002-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00003-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00004-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00005-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00006-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00007-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00008-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00009-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00010-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00011-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00012-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00013-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00014-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00015-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00016-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00017-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00018-of-00020',\n",
      " 'generate_tfrecords_zip/valid_00019-of-00020']\n",
      "['generate_tfrecords_zip/test_00000-of-00020',\n",
      " 'generate_tfrecords_zip/test_00001-of-00020',\n",
      " 'generate_tfrecords_zip/test_00002-of-00020',\n",
      " 'generate_tfrecords_zip/test_00003-of-00020',\n",
      " 'generate_tfrecords_zip/test_00004-of-00020',\n",
      " 'generate_tfrecords_zip/test_00005-of-00020',\n",
      " 'generate_tfrecords_zip/test_00006-of-00020',\n",
      " 'generate_tfrecords_zip/test_00007-of-00020',\n",
      " 'generate_tfrecords_zip/test_00008-of-00020',\n",
      " 'generate_tfrecords_zip/test_00009-of-00020',\n",
      " 'generate_tfrecords_zip/test_00010-of-00020',\n",
      " 'generate_tfrecords_zip/test_00011-of-00020',\n",
      " 'generate_tfrecords_zip/test_00012-of-00020',\n",
      " 'generate_tfrecords_zip/test_00013-of-00020',\n",
      " 'generate_tfrecords_zip/test_00014-of-00020',\n",
      " 'generate_tfrecords_zip/test_00015-of-00020',\n",
      " 'generate_tfrecords_zip/test_00016-of-00020',\n",
      " 'generate_tfrecords_zip/test_00017-of-00020',\n",
      " 'generate_tfrecords_zip/test_00018-of-00020',\n",
      " 'generate_tfrecords_zip/test_00019-of-00020']\n"
     ]
    }
   ],
   "source": [
    "pprint.pprint(train_tfrecord_filenames)\n",
    "pprint.pprint(valid_tfrecord_filenames)\n",
    "pprint.pprint(test_tfrecord_fielnames)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[ 2.5150437   1.0731637   0.5574401  -0.17273512 -0.6129126  -0.01909157\n",
      "  -0.5710993  -0.02749031]\n",
      " [ 2.5150437   1.0731637   0.5574401  -0.17273512 -0.6129126  -0.01909157\n",
      "  -0.5710993  -0.02749031]\n",
      " [ 2.5150437   1.0731637   0.5574401  -0.17273512 -0.6129126  -0.01909157\n",
      "  -0.5710993  -0.02749031]], shape=(3, 8), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[5.00001]\n",
      " [5.00001]\n",
      " [5.00001]], shape=(3, 1), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[ 2.5150437   1.0731637   0.5574401  -0.17273512 -0.6129126  -0.01909157\n",
      "  -0.5710993  -0.02749031]\n",
      " [ 2.5150437   1.0731637   0.5574401  -0.17273512 -0.6129126  -0.01909157\n",
      "  -0.5710993  -0.02749031]\n",
      " [ 0.8015443   0.27216142 -0.11624393 -0.20231152 -0.5430516  -0.02103962\n",
      "  -0.5897621  -0.08241846]], shape=(3, 8), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[5.00001]\n",
      " [5.00001]\n",
      " [3.226  ]], shape=(3, 1), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "expected_features = {\n",
    "    \"input_features\": tf.io.FixedLenFeature([8], dtype=tf.float32),\n",
    "    \"label\": tf.io.FixedLenFeature([1], dtype=tf.float32)\n",
    "}\n",
    "\n",
    "def parse_example(serialized_example):\n",
    "    example = tf.io.parse_single_example(serialized_example,\n",
    "                                         expected_features)\n",
    "    return example[\"input_features\"], example[\"label\"]\n",
    "\n",
    "def tfrecords_reader_dataset(filenames, n_readers=5,\n",
    "                             batch_size=32, n_parse_threads=5,\n",
    "                             shuffle_buffer_size=10000):\n",
    "    dataset = tf.data.Dataset.list_files(filenames)\n",
    "    dataset = dataset.repeat()\n",
    "    dataset = dataset.interleave(\n",
    "        lambda filename: tf.data.TFRecordDataset(\n",
    "            filename, compression_type = \"GZIP\"),\n",
    "        cycle_length = n_readers\n",
    "    )\n",
    "    dataset.shuffle(shuffle_buffer_size)\n",
    "    dataset = dataset.map(parse_example,\n",
    "                          num_parallel_calls=n_parse_threads)\n",
    "    dataset = dataset.batch(batch_size)\n",
    "    return dataset\n",
    "\n",
    "tfrecords_train = tfrecords_reader_dataset(train_tfrecord_filenames,\n",
    "                                           batch_size = 3)\n",
    "for x_batch, y_batch in tfrecords_train.take(2):\n",
    "    print(x_batch)\n",
    "    print(y_batch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "tfrecords_train_set = tfrecords_reader_dataset(\n",
    "    train_tfrecord_filenames, batch_size = batch_size)\n",
    "tfrecords_valid_set = tfrecords_reader_dataset(\n",
    "    valid_tfrecord_filenames, batch_size = batch_size)\n",
    "tfrecords_test_set = tfrecords_reader_dataset(\n",
    "    test_tfrecord_fielnames, batch_size = batch_size)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "348/348 [==============================] - 2s 6ms/step - loss: 1.9641 - val_loss: 1.3548\n",
      "Epoch 2/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.9449 - val_loss: 1.1665\n",
      "Epoch 3/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.7758 - val_loss: 1.0737\n",
      "Epoch 4/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.6887 - val_loss: 1.0104\n",
      "Epoch 5/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.6294 - val_loss: 0.9645\n",
      "Epoch 6/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.5800 - val_loss: 0.9270\n",
      "Epoch 7/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.5613 - val_loss: 0.8938\n",
      "Epoch 8/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.5289 - val_loss: 0.8619\n",
      "Epoch 9/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.5253 - val_loss: 0.8352\n",
      "Epoch 10/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.5177 - val_loss: 0.8116\n",
      "Epoch 11/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.5112 - val_loss: 0.7967\n",
      "Epoch 12/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.5143 - val_loss: 0.7788\n",
      "Epoch 13/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4845 - val_loss: 0.7690\n",
      "Epoch 14/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4723 - val_loss: 0.7548\n",
      "Epoch 15/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4560 - val_loss: 0.7447\n",
      "Epoch 16/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.4514 - val_loss: 0.7349\n",
      "Epoch 17/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4522 - val_loss: 0.7246\n",
      "Epoch 18/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4453 - val_loss: 0.7159\n",
      "Epoch 19/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4397 - val_loss: 0.7070\n",
      "Epoch 20/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4378 - val_loss: 0.7035\n",
      "Epoch 21/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4253 - val_loss: 0.6967\n",
      "Epoch 22/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4309 - val_loss: 0.6929\n",
      "Epoch 23/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4189 - val_loss: 0.6868\n",
      "Epoch 24/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4279 - val_loss: 0.6812\n",
      "Epoch 25/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4243 - val_loss: 0.6752\n",
      "Epoch 26/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4183 - val_loss: 0.6687\n",
      "Epoch 27/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4173 - val_loss: 0.6646\n",
      "Epoch 28/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4117 - val_loss: 0.6624\n",
      "Epoch 29/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4119 - val_loss: 0.6572\n",
      "Epoch 30/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4061 - val_loss: 0.6533\n",
      "Epoch 31/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4131 - val_loss: 0.6507\n",
      "Epoch 32/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4255 - val_loss: 0.6474\n",
      "Epoch 33/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4306 - val_loss: 0.6424\n",
      "Epoch 34/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4362 - val_loss: 0.6386\n",
      "Epoch 35/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4252 - val_loss: 0.6381\n",
      "Epoch 36/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4107 - val_loss: 0.6342\n",
      "Epoch 37/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.4054 - val_loss: 0.6320\n",
      "Epoch 38/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3889 - val_loss: 0.6279\n",
      "Epoch 39/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3965 - val_loss: 0.6242\n",
      "Epoch 40/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3928 - val_loss: 0.6198\n",
      "Epoch 41/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3874 - val_loss: 0.6145\n",
      "Epoch 42/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3869 - val_loss: 0.6120\n",
      "Epoch 43/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.3829 - val_loss: 0.6105\n",
      "Epoch 44/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.3819 - val_loss: 0.6072\n",
      "Epoch 45/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3755 - val_loss: 0.6042\n",
      "Epoch 46/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.3775 - val_loss: 0.6026\n",
      "Epoch 47/100\n",
      "348/348 [==============================] - 2s 5ms/step - loss: 0.3814 - val_loss: 0.5990\n",
      "Epoch 48/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3779 - val_loss: 0.5961\n",
      "Epoch 49/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3750 - val_loss: 0.5922\n",
      "Epoch 50/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3766 - val_loss: 0.5925\n",
      "Epoch 51/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3678 - val_loss: 0.5889\n",
      "Epoch 52/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3742 - val_loss: 0.5880\n",
      "Epoch 53/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3649 - val_loss: 0.5849\n",
      "Epoch 54/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3777 - val_loss: 0.5833\n",
      "Epoch 55/100\n",
      "348/348 [==============================] - 1s 4ms/step - loss: 0.3811 - val_loss: 0.5814\n",
      "Epoch 56/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3781 - val_loss: 0.5772\n",
      "Epoch 57/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3866 - val_loss: 0.5766\n",
      "Epoch 58/100\n",
      "348/348 [==============================] - 1s 3ms/step - loss: 0.3855 - val_loss: 0.5759\n"
     ]
    }
   ],
   "source": [
    "model = keras.models.Sequential([\n",
    "    keras.layers.Dense(30, activation='relu',\n",
    "                       input_shape=[8]),\n",
    "    keras.layers.Dense(1),\n",
    "])\n",
    "model.compile(loss=\"mean_squared_error\", optimizer=\"sgd\")\n",
    "callbacks = [keras.callbacks.EarlyStopping(\n",
    "    patience=5, min_delta=1e-2)]\n",
    "\n",
    "history = model.fit(tfrecords_train_set,\n",
    "                    validation_data = tfrecords_valid_set,\n",
    "                    steps_per_epoch = 11160 // batch_size,\n",
    "                    validation_steps = 3870 // batch_size,\n",
    "                    epochs = 100,\n",
    "                    callbacks = callbacks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "161/161 [==============================] - 0s 2ms/step - loss: 0.4429\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.4428980045066857"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(tfrecords_test_set, steps = 5160 // batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
